<?php
App::uses('AppController', 'Controller');
/**
 * Bolsas Controller
 *
 * @property Bolsa $Bolsa
 * @property SessionComponent $Session
 */
class BolsaController extends AppController {
    
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow("visualizar");
    }

/**
 * index method
 *
 * @return void
 */
	public function index() {
		if ($this->request->is('post')) {
                    
                    if ($this->Auth->login()) {				
                        $this->redirect($this->Auth->redirect());
                    } 
                    else {
                        $this->Session->setFlash(__('Login ou senha inválidos, por favor, tente novamente.'));
                    }
                }
                
                $this->Bolsa->recursive = 0;
                $bolsas = $this->Bolsa->find('all', array("order" => "Bolsa.created DESC", "limit" => 6));
		$this->set(compact('bolsas'));
        }
        
        public function logout(){
            $this->redirect($this->Auth->logout());
        }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function visualizar($id = null) {
		if (!$this->Bolsa->exists($id)) {
			throw new NotFoundException(__('Invalid bolsa'));
		}
		$options = array('conditions' => array('Bolsa.' . $this->Bolsa->primaryKey => $id));
		$this->set('bolsa', $this->Bolsa->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function adicionar() {
            die;
		if ($this->request->is('post')) {
			$this->Bolsa->create();
			if ($this->Bolsa->save($this->request->data)) {
				$this->Session->setFlash(__('The bolsa has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The bolsa could not be saved. Please, try again.'));
			}
		}
		$grauDeEnsinos = $this->Bolsa->GrauDeEnsino->find('list');
		$cursos = $this->Bolsa->Curso->find('list');
		$periodos = $this->Bolsa->Periodo->find('list');
		$linguas = $this->Bolsa->Lingua->find('list');
		$tipoBolsas = $this->Bolsa->TipoBolsa->find('list');
		$usuarios = $this->Bolsa->Usuario->find('list');
		$this->set(compact('grauDeEnsinos', 'cursos', 'periodos', 'linguas', 'tipoBolsas', 'usuarios'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function editar($id = null) {
            die;
		if (!$this->Bolsa->exists($id)) {
			throw new NotFoundException(__('Invalid bolsa'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Bolsa->save($this->request->data)) {
				$this->Session->setFlash(__('The bolsa has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The bolsa could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Bolsa.' . $this->Bolsa->primaryKey => $id));
			$this->request->data = $this->Bolsa->find('first', $options);
		}
		$grauDeEnsinos = $this->Bolsa->GrauDeEnsino->find('list');
		$cursos = $this->Bolsa->Curso->find('list');
		$periodos = $this->Bolsa->Periodo->find('list');
		$linguas = $this->Bolsa->Lingua->find('list');
		$tipoBolsas = $this->Bolsa->TipoBolsa->find('list');
		$usuarios = $this->Bolsa->Usuario->find('list');
		$this->set(compact('grauDeEnsinos', 'cursos', 'periodos', 'linguas', 'tipoBolsas', 'usuarios'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function excluir($id = null) {
            die;
		$this->Bolsa->id = $id;
		if (!$this->Bolsa->exists()) {
			throw new NotFoundException(__('Invalid bolsa'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Bolsa->delete()) {
			$this->Session->setFlash(__('Bolsa deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Bolsa was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
